/*
 * Created by: Peter Weissbrod
 * Created: Monday, January 21, 2008
 */
using System;
using PostSharp.Laos;
using PostSharp.UserSamples.Exceptions;
using PostSharp.UserSamples.Utils;

namespace PostSharp.UserSamples.Validators
{
    /// <summary>
    /// Throws <typeparamref name="ValidationException">ValidationException</typeparamref> if the value is null.
    /// </summary>
    [Serializable]
    public class NotNullValidator:OnMethodBoundaryAspect
    {
        /// <summary>
        /// This aspect is designed for property setters
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        public override bool CompileTimeValidate(System.Reflection.MethodBase method)
        {
            return CompileTimeValidateScenario.ValidateForPublicPropertySetter(method);
        }
        /// <summary>
        /// Throws exception if the value is null
        /// </summary>
        /// <param name="eventArgs"></param>
        public override void OnEntry(MethodExecutionEventArgs eventArgs)
        {
            //ignore call from reflection
            if (eventArgs.GetArguments() != null)
            {
                //check for null
                if (eventArgs.GetArguments()[0] == null)
                    throw new ValidationException("Value was null.", eventArgs);
            }
            base.OnEntry(eventArgs);
        }
    }
}